//
//  ResultViewController.m
//  faceDemo
//
//  Created by golang on 2022/4/6.
//  Copyright © 2022 hisign. All rights reserved.
//

#import "ResultViewController.h"
#import <HSFaceDetector/LiveHeader.h>
#import "SABusinessInformationVC.h"

@interface ResultViewController ()
@property (nonatomic, weak)UIView *bgView;
@property (nonatomic, weak)UILabel *messageLab;
@property (nonatomic, weak)UIImageView *picView;
@end

@implementation ResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self addCompletedView];
}

- (void)initView{
    
    UIView *bgView = [[UIView alloc]initWithFrame:self.view.frame];
    bgView.backgroundColor = HSBlueColor;
    [self.view addSubview:bgView];
    self.bgView = bgView;
    
    UIImageView *picView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREENWIDTH/8*2, SCREENWIDTH/4,
                                                                SCREENWIDTH/2, SCREENWIDTH/3*2)];
    picView.layer.borderColor = HSGreenColor.CGColor;
    picView.layer.borderWidth = 2;
    [bgView addSubview:picView];
    self.picView = picView;
    
    UILabel *messageLab = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.picView.frame)+20, SCREENWIDTH, 44)];
    messageLab.textColor = HSGreenColor;
    messageLab.textAlignment = NSTextAlignmentCenter;
    messageLab.font = [UIFont systemFontOfSize:25];
    [bgView addSubview:messageLab];
    self.messageLab = messageLab;

}

- (void)addCompletedView{
    if (self.errorCode == IV_ERROR_NONE) {
//        self.messageLab.text = @"活体通过";
        [Prompt popPromptViewWithMsg:@"活体通过" duration:2];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"OcrSuccess" object:nil];
    }else if (self.errorCode == IV_ERROR_NOTLIVE){
//        self.messageLab.text = @"假体未通过";
        self.messageLab.textColor = [UIColor redColor];
        [Prompt popPromptViewWithMsg:@"假体未通过" duration:2];
    }else {
        self.messageLab.text = self.errorMessage;
        self.messageLab.textColor = [UIColor redColor];
    }
    if (self.liveImage != nil) {
        self.picView.image = self.liveImage;
    }else {
        self.picView.image = [UIImage imageNamed:@"HSFaceData.bundle/image/fail.png"];
    }
    
    [self quitAction];
//    UIButton *quitBtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREENWIDTH/10, CGRectGetMaxY(self.messageLab.frame)+64, SCREENWIDTH/10*8, 44)];
//
//    quitBtn.backgroundColor = HSGreenColor;
//    [quitBtn setTitle:@"退出" forState:UIControlStateNormal];
//    [quitBtn setTitleColor:HSBlueColor forState:UIControlStateNormal];
//    [quitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
//    [quitBtn addTarget:self action:@selector(quitAction) forControlEvents:UIControlEventTouchUpInside];
//    [self.bgView addSubview:quitBtn];
    
}

- (void)quitAction{
    NSString *controllerName = @"SABusinessInformationVC";
    NSArray *temArray = self.navigationController.viewControllers;
    BOOL pop = NO; //!<记录是否已经跳转
    for(UIViewController *temVC in temArray)
    {
        if ([temVC isKindOfClass:NSClassFromString(controllerName)])
        {
            pop = YES;
            [self.navigationController popToViewController:temVC animated:YES];
        }
    }
    
    if (!pop)
    {
        [self pushVC:controllerName :YES];
    }
}

-(void)pushVC:(NSString *)controllerName :(BOOL)animated
{
    [self.navigationController pushViewController:(UIViewController *)[NSClassFromString(controllerName) new] animated:animated];
}

@end
